/*
 * Dépendances:
 * 	- Jquery: >= 1.5.1
 * 
 * Events:
 * - jmap_init_core:	when core initializatiion will be performed
 * - jmap_init: 		when all initialization will be performed, before map loaded
 * - jmap_loaded:		after map will be load
 * - jmap_marker_add:	after marker will be loaded
 * 
 * Events google reported
 * - jmap_map_(google.maps.mapEvent)
 * 
 */

(function( $ ){
	
	$.fn.jmap = function( method ) {
		
		// Method calling logic
		if ( $.fn.jmap.methods[method] ) {
			return $.fn.jmap.methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return $.fn.jmap.construct._construct.apply( this, arguments );
		} else {
			alert( 'Method ' +  method + ' does not exist on jQuery.jmap' );
		}
	};
	
	$.fn.jmap.defaultOptions = {
		width : 400, /*- option -*/
		height: 300, /*- option -*/
		editor: true, /*- option -*/
		
		mapTypeId : google.maps.MapTypeId.ROADMAP,
		zoom : 4,
	};
	
	
	// Constructeur
	//------------------------------------------------------------------
	$.fn.jmap.construct = {
		/*+
		 * =name : pilot
		 * =description : 
		 * =params: options
		 * =return: jquery objects
		 * +*/
		_construct : function( options ) {
			return this.each(function() {
				var parent	= this;
				
				if( $(parent).data('init') )
					return false;
				
				var defaultOptions = $.fn.jmap.defaultOptions;
				
				if( options.mapType )
					options.mapTypeId = google.maps.MapTypeId[options.mapType];
				if( options.mapCenter )
					options.center = new google.maps.LatLng(options.mapCenter[0],options.mapCenter[1]);
				
				$.extend(defaultOptions,options);
				
				// Initialisation des objets
				$(parent).data('options',defaultOptions);
				$(parent).data('markers', []);
				
				for( var i=0; i<$.fn.jmap.modules.length; i++ ) {
					if( $.fn.jmap.init[$.fn.jmap.modules[i]] )
						$.fn.jmap.init[$.fn.jmap.modules[i]].apply(parent);
				}
				$(parent).trigger( "jmap_init" );
				
				$(parent).find('.jmap-map').each(function(){
					var map = new google.maps.Map( this, defaultOptions );
					$(parent).data('map', map );
					$.fn.jmap.privates.initMapEvents.apply(parent,[map]);
				});
				$(parent).trigger( "jmap_load", $(parent).data('map') );
				
				$(parent).data('init');
			});
		},
	};
	
	
	// Initialisation
	//------------------------------------------------------------------
	$.fn.jmap.init = {
		core : function() {
			var parent = this;
			$(parent).empty()
				.append('<div class="jmap-map"></div>');
			
			$(parent).find('.jmap-map')
				.css('width',$(parent).data('options').width)
				.css('height',$(parent).data('options').height)
				.css('float','left');
			
			$(this).trigger('jmap_init_core'); /*- event : when core initializatiion will be performed -*/
		},
	};
	
	
	// Méthodes publiques
	//------------------------------------------------------------------
	$.fn.jmap.methods = {
		
		/*+
		 * =name : addMarker
		 * =description : add marker to map
		 * =params : jmapMarker extend google.maps.Marker
		 * =return : jquery objects
		 * =event : jmap_marker_add( jqueryEvent, marker )
		 * +*/
		addMarker : function( marker ) {
			return $(this).each( function() {
				var parent = this;
				marker.map = $(parent).data('map');
				if( marker.coords )
					marker.position = new google.maps.LatLng(marker.coords[0],marker.coords[1]);
				marker.index = $(parent).data('markers').length;
				
				var TheMarker = new google.maps.Marker(marker);
				
				$(parent).trigger('jmap_marker_add', TheMarker ); /*- event : after marker will be added -*/
				
			});
		},
		
		/*+
		 * =name : delMarker
		 * =description : del marker from map
		 * =params : jmapMarker
		 * =return : jquery objects
		 * =event : jmap_marker_del( jqueryEvent, marker )
		 * +*/
		addMarker : function( marker ) {
			return $(this).each( function() {
				var parent = this;
				
				marker.setMap(null);
				$(parent).trigger('jmap_marker_del', marker ); /*- event : after marker will be deleted -*/
			});
		},
	};
	
	
	// Méthodes privées
	//------------------------------------------------------------------
	$.fn.jmap.privates = {
		
		// Initialisation des évents googlemap
		initMapEvents: function( map ) {
			var parent = this;
			var events = $.fn.jmap.gmapEvents.map;
			
			for ( var i=0; i<events.length; i++) {
				
				(function(parent,map,gmapEvent){
					google.maps.event.addListener( map, gmapEvent, function(e) {
						$(parent).trigger( "jmap_map_"+gmapEvent, e );
					});
				})(parent,map,events[i]);
			}
		},
	};
	
	// Listing des events
	$.fn.jmap.gmapEvents = {
		map : [
			'bounds_changed',
			'center_changed',
			'click',
			'dblclick',
			'drag',
			'dragend',
			'dragstart',
			'heading_changed',
			'idle',
			'maptypeid_changed',
			'mousemove',
			'mouseout',
			'mouseover',
			'projection_changed',
			'resize',
			'rightclick',
			'tilesloaded',
			'tilt_changed',
			'zoom_changed',
		],
	};
	
	// Informations sur le module
	//------------------------------------------------------------------
	$.fn.jmap.modules = ['core'];
	$.fn.jmap.infos = {
		core: {
			conflicts: [],
			dependencies: [ ['jquery','>= 1.5.1'], ],
			version: '0.110615',
		}
	};
	
})( jQuery );
